<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This page apply changes on my preferences.
 *
 * @package   blocks-email_list
 * @author    Toni Mas <toni.mas@uib.dot.es>
 * @copyright 2006 by <toni.mas at uib dot es>.
 * @license   The source code packaged with this file is Free Software, Copyright (C) 2006 by
 *          <toni.mas at uib dot es>.
 *          It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
 *          You can get copies of the licenses here:
 * 		                   http://www.affero.org/oagpl.html
 *          AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
 * @uses $CFG
 * @uses $DB
 * @uses $OUTPUT
 * @uses $PAGE
 * @uses $USER
 */

require_once('../../../config.php');
require_once($CFG->dirroot.'/blocks/email_list/email/lib.php');
require_once($CFG->dirroot.'/blocks/email_list/email/preferences_form.php');

$courseid	= optional_param('id', SITEID, PARAM_INT);		// Course ID

global $CFG, $DB, $OUTPUT, $PAGE, $USER;

// If defined course to view
if (! $course = $DB->get_record('course', array('id' => $courseid))) {
    print_error('courseavailablenot', 'moodle');
}

require_login($course->id, false); // No autologin guest

if (! email_is_enabled_email_list($course->id)) {
    print_error('blocknotavailable', 'block_email_list');
}

// Can edit settings?
if (! has_capability('block/email_list:editsettings', $PAGE->context)) {
    print_error('forbiddeneditsettings', 'block_email_list', $CFG->wwwroot
               .'/blocks/email_list/email/index.php?id='.$course->id);
}

// Security enable user's preference
if (empty($CFG->email_trackbymail) and empty($CFG->email_marriedfolders2courses) ) {
    redirect($CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid,
             get_string('preferencesnotenable', 'block_email_list', '2'));
}

// Options for new mail and new folder
$options = new stdClass();
$options->id = $courseid;

/// Print the page header

$stremail  = get_string('name', 'block_email_list');

$PAGE->set_title("$course->shortname: $stremail");
$PAGE->set_heading($course->fullname);
$PAGE->set_cacheable(true);
$PAGE->set_url('/blocks/email_list/email/preferences.php?courseid='. $course->id);
$PAGE->navbar->add(get_string('nameplural', 'block_email_list'), 'index.php?id='.$course->id);
$PAGE->navbar->add(get_string('name', 'block_email_list'));
$PAGE->requires->css('/blocks/email_list/email/email.css');
$PAGE->requires->css('/blocks/email_list/email/treemenu.css');
$PAGE->requires->css('/blocks/email_list/email/tree.css');
$PAGE->requires->js('/blocks/email_list/email/treemenu.js');
$PAGE->requires->js('/blocks/email_list/email/email.js');

$mform = new preferences_form('preferences.php', array('id' => $courseid));

if ($mform->is_cancelled()) {

    // Only redirect
    redirect($CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid, '', 0);

} else if ($form = $mform->get_data()) {

    // Add log for one course
    add_to_log($courseid, 'email_list', 'edit preferences', 'preferences.php', 'Edit my preferences', 0, $USER->id);

    $preference = new stdClass();

    if ($DB->record_exists('block_email_list_preference', array('userid' => $USER->id))) {

        if (! $preference = $DB->get_record('block_email_list_preference', array('userid' => $USER->id))) {
            print_error('failreadingpreferences', 'block_email_list',
                        $CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid);
        }

        // Security
        if ( $CFG->email_trackbymail ) {
            $preference->trackbymail = $form->trackbymail;
        } else {
            $preference->trackbymail = 0;
        }

        // Security
        if ( $CFG->email_marriedfolders2courses ) {
            $preference->marriedfolders2courses = $form->marriedfolders2courses;
        } else {
            $preference->marriedfolders2courses = 0;
        }

        $preference->emailrefreshrate = $form->emailrefreshrate;

        if ($DB->update_record('block_email_list_preference', $preference) ) {
            redirect($CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid,
                     get_string('savedpreferences', 'block_email_list'), '2');
        }
    } else {

        $preference->userid = $USER->id;

        // Security
        if ( $CFG->email_trackbymail ) {
            $preference->trackbymail = $form->trackbymail;
        } else {
            $preference->trackbymail = 0;
        }
        // Security
        if ( $CFG->email_marriedfolders2courses ) {
            $preference->marriedfolders2courses = $form->marriedfolders2courses;
        } else {
            $preference->marriedfolders2courses = 0;
        }

        $preference->emailrefreshrate = $form->emailrefreshrate;

        if ($DB->insert_record('block_email_list_preference', $preference) ) {
            redirect($CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid,
                     get_string('savedpreferences', 'block_email_list'), '2');
        }
    }

    print_error('errorsavepreferences', 'block_email_list',
          $CFG->wwwroot.'/blocks/email_list/email/index.php?id='.$courseid);
} else {

    // Get my preferences, if I have.
    $preferences = $DB->get_record('block_email_list_preference', array('userid' => $USER->id));

    if ($preferences === false) {
        $preferences = new stdClass();
    }

    // Add course
    $preferences->id = $courseid;

    // Set data
    $mform->set_data($preferences);
}

echo $OUTPUT->header();

// Print principal table. This have 2 columns . . .  and possibility to add right column.
echo '<table id="layout-table">
            <tr>';


// Print "blocks" of this account
echo '<td style="width: 180px;" id="left-column">';
email_printblocks($USER->id, $courseid);

// Close left column
echo '</td>';

// Print principal column
echo '<td id="middle-column">';

// Print block
echo $OUTPUT->heading('');

$mform->display();

echo '<div>&#160;</div>';

// Close principal column
echo '</td>';

// Close table
echo '</tr>
			</table>';

echo $OUTPUT->footer();